﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace KKUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OpenTrend()
        {
            Process process = new Process();
            process.StartInfo.WorkingDirectory = Application.StartupPath;
            process.StartInfo.FileName = "xysc.exe";
            process.Start();

            Application.Exit();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;

            try
            {
                string filename = "update.zip";

                System.Net.WebClient client = new System.Net.WebClient();

                client.DownloadFile("http://www.kkcai.net/update/xysc.zip", filename);

                #region 解压

                using (ZipInputStream s = new ZipInputStream(File.OpenRead(filename)))
                {

                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);

                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(directoryName);
                        }

                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(theEntry.Name))
                            {

                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                string ver = new WebUtils().DoGet("http://www.kkcai.net/update/VerSC.txt");

                IniFileHelper.IniWriteValue("license", "ver", ver, Application.StartupPath + "\\cfg.ini");
            }
            catch
            {
                MessageBox.Show("更新失败!请检查网络连接!", "提示");

                btnUpdate.Enabled = true;

                return;
            }

            OpenTrend();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string localVer = IniFileHelper.IniReadValue("license", "ver", Application.StartupPath + "\\cfg.ini");

            string serverVer = new WebUtils().DoGet("http://www.kkcai.net/update/VerSC.txt");

            if (localVer != serverVer)
            {
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                OpenTrend();
            }
        }
    }
}
